﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using PeaceBlogData;
using PeaceBlogData.Tables;

namespace PeaceBlog.Controllers.API
{
    public class PostsController : ApiController
    {
        private PeaceBlogEntity db = new PeaceBlogEntity();

        // GET: api/Posts
        [HttpGet]
        public async Task<List<Posts>> GetPosts()
        {

            return await db.GetAllPosts();
        }

        // GET: api/Posts
        [HttpGet]
        public PagedList.IPagedList<Posts> Search(string query,int PageNumber)
        {
            int PageSize;
            if (int.TryParse(SiteHelpers.GetSetting("PageSize"), out PageSize))
                return db.Search(query,PageNumber, PageSize);
            else
                return db.Search(query, PageNumber, 10);
        }

        // GET: api/Posts
        [HttpGet]
        public PagedList.IPagedList<Posts> GetPosts(int PageNumber)
        {
            int PageSize;
            if (int.TryParse(SiteHelpers.GetSetting("PageSize"), out PageSize))
                return db.GetAllPosts(PageNumber, PageSize);
            else
                return db.GetAllPosts(PageNumber, 10);
        }
        
        // GET: api/Posts
        [HttpGet]
        public List<Posts> GetPostsFromAuthor(int AuthorID)
        {
            return db.GetPostsFromAuthor(AuthorID);
        }

        // GET: api/Posts/5
        [HttpGet]
        public async Task<Posts> GetPost(Guid id)
        {
            var posts = await db.GetPost(id);
            if (posts == null)
                return null;

            return posts;
        }

        [HttpPost]
        public async Task<Posts> GetPost(string Id)
        {
            var posts = await db.GetPostFromURL(Id);
            if (posts == null)
                return null;

            return posts;
        }

        // PUT: api/Posts/5
        [HttpPut]
        public async Task<IHttpActionResult> UpdatePost(Posts posts)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);
            try
            {
                await db.UpdatePost(posts);
            }
            catch (DbUpdateConcurrencyException)
            {
                return NotFound();
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Posts
        [HttpPost]
        public async Task<IHttpActionResult> AddPost(Posts post)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);


            try
            {
                await db.AddPost(post);
            }
            catch (DbUpdateException)
            {
                return BadRequest("Kategori Eklenemedi!");
            }

            return Ok();
        }

        // DELETE: api/Posts/5
        [HttpDelete]
        public async Task<IHttpActionResult> DeletePosts(Guid id)
        {
            try
            {
                await db.DeletePost(id);
            }
            catch
            {
                return NotFound();
            }

            return Ok("Post Silindi");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}